package com.itheima.job.service;

import com.itheima.job.dto.EmailDTO;
import com.itheima.job.exception.SystemException;
import com.itheima.job.result.Result;
import com.itheima.job.result.enums.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

/**
 * @author byZhao
 * @motto Talk is cheap . Show me the code!
 * @modify 2021-11-27 11:40
 * @description
 */
@Service
public class EmailService {

    @Autowired
    JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    private String from;

    /**
     * 发送邮件
     *
     * @param emailDTO 邮件DTO
     * @return Result
     */
    public Result sendEmail(EmailDTO emailDTO) {


        try {
            int i = 1 / 0;
        } catch (Exception e) {
            throw new SystemException("系统出问题了哈哈哈.....");
        }


        SimpleMailMessage message = new SimpleMailMessage();
        message.setTo(emailDTO.getTo());
        message.setFrom(from);
        message.setSubject(emailDTO.getSubject());
        message.setText(emailDTO.getText());
        javaMailSender.send(message);
        return Result.ok(ResultEnum.MAIL_SEND_MAIL_SUCCESS);

    }


}
